package com.example.exchange.config;
 
import org.springframework.amqp.core.*;

import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;

@Configuration
public class ApiRabbitMqConfig  implements RabbitListenerConfigurer {
 

    private final String queue = "deviceQueue";
 

    private final String exchange ="deviceExchange";
 

    private final String routingKey ="deviceRoutingKey";


 
    /**
     * 创建队列
     *
     * @return
     */
    @Bean
    public Queue getApiQueue() {
        /*
         * name(必填): 创建消息队列 队列名称：CHANNEL_API_QUEUE
         * durable: 是否持久化,默认false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
         * exclusive: 默认false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
         * autoDelete: 是否自动删除，默认false，当没有生产者或者消费者使用此队列，该队列会自动删除。
         */
        return new Queue(queue, true);
    }
 
    /**
     * 创建直流交换器
     *
     * @return
     */
    @Bean
    TopicExchange exchange() {
        return new TopicExchange(exchange);
    }
 
    /**
     * 交换器绑定队列
     *
     * @return
     */
    @Bean
    public Binding bindingQueueToExchange() {
        /*
         * 将队列绑定到交换器上, 并设置路由键
         */
        return BindingBuilder.bind(getApiQueue()).to(exchange()).with(routingKey);
    }

    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
        rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
    }

    /**
     * 定义支持json的RabbitTemplate
     * @param connectionFactory
     * @return
     */
    @Bean
    public RabbitTemplate jsonRabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        //重写发送端的消息转换器，来替换默认的SimpleMessageConverter
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        return template;
    }


    @Bean
    MessageHandlerMethodFactory messageHandlerMethodFactory(){
        DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
        messageHandlerMethodFactory.setMessageConverter(mappingJackson2MessageConverter());
        return messageHandlerMethodFactory;
    }

    @Bean
    public MappingJackson2MessageConverter mappingJackson2MessageConverter(){
        return  new MappingJackson2MessageConverter();
    }
}